/*
Java source file
Copyright: Umschalt.com 2012
*/
package com.david.documentation.web.view.components;

import com.david.documentation.domain.entities.AutocompleteData;
import org.apache.wicket.Response;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AbstractAutoCompleteRenderer;
import org.apache.wicket.markup.html.form.FormComponent;

public class AutocompleteDataRenderer extends AbstractAutoCompleteRenderer<AutocompleteData>{

	private FormComponent<?> additionalInfo;

	public AutocompleteDataRenderer(FormComponent<?> additionalInfo ){
		this.additionalInfo = additionalInfo;
		additionalInfo.setOutputMarkupId(true);
	}

	@Override
	protected void renderChoice(AutocompleteData object, Response response, String criteria) {
		String tmp = object.getCategoryOrTag();

		if (object.getCategoryOrTag().equals(AutocompleteDataBehavior.KATGEORY_VALUE)){
			tmp =  "<span class='category'>" +object.getCategoryOrTag() + "</span>";
		} else if (object.getCategoryOrTag().equals(AutocompleteDataBehavior.TAG_VALUE)){
			tmp =  "<span class='tag'>" +object.getCategoryOrTag() + "</span>";
		}
		response.write(object.getName() + tmp);
	}

	@Override
	protected String getTextValue(AutocompleteData object) {
		return object.getName();
	}


	@Override
  	protected CharSequence getOnSelectJavascriptExpression(AutocompleteData data) {
		StringBuilder js = new StringBuilder();
		js.append("wicketGet('").append(additionalInfo.getMarkupId()).append("').value ='" + data.getCategoryOrTag() + "';");
		js.append("input");
		
		return js.toString();
	}

}
